/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package au.com.gworks.jump.app.wiki.client.administration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.javaongems.client.Gem;
import org.javaongems.client.forms.Form;
import org.javaongems.client.forms.FormController;
import org.javaongems.client.stdctrls.Image;
import org.javaongems.client.winctrls.ArrayListViewDataSource;
import org.javaongems.client.winctrls.ListView;
import org.javaongems.client.winctrls.ListViewDataSource;
import org.javaongems.client.winctrls.ListViewListener;


import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.DockPanel;

public class ControlPanel {
	static public class SettingsForm extends Form {
		private DockPanel outerPanel = new DockPanel();
		private ListView listView = new ListView();
		private Controller controller;
		
		public SettingsForm(Controller ctrlr) {
			super(ctrlr);
			controller = ctrlr;
			initialiseLayout();
			initialiseWidgets();
			initialiseListeners();
		}
		
		private void initialiseLayout() {
			setCaption("Control panel");
			add(outerPanel);
			outerPanel.add(listView, DockPanel.CENTER);
			DOM.setAttribute(getElement(), "id", "jumpControlPanelWindow");
		}
		
		private void initialiseWidgets() {
			String imgPfx = Gem.stone.getApplImageCtx();
			com.google.gwt.user.client.ui.Image icon = new Image(imgPfx + "administer-x16.png");
			setFormIcon(icon);
			listView.setDataSource((ListViewDataSource)controller.getModel());
		}
		
		private void initialiseListeners() {
			setFormActionListener(controller);
			listView.setViewListener(controller);
		}
		
		public void show() {
			super.show();
			paint();
		}

		public void refresh() {
			super.refresh();
			paint();
		}
		
		private void paint() {
			listView.render();
			resizeListView();
		}
		
		/*pkg*/ void resizeListView() {		// workaround of bug in listview resizing
			int width = getStatusBarWidth();
			int lbTop = listView.getAbsoluteTop();
			int sbTop = getStatusBarTop();
			int height = sbTop - lbTop;
			listView.onResize(width, height);
		}
	}
	
	static public class Controller extends FormController implements ListViewListener {
		private SettingsForm window;
		private ArrayList data = new ArrayList();
		private ArrayListViewDataSource lvDataSet;
		
		public void setUp(Map config) {
			super.setUp(config);
			initialiseControls();
			window = new SettingsForm(this);
			setForm(window);
			int[] leftTopWidthHeight = (int[]) config.get("top.left.width.height.array");
			if (leftTopWidthHeight == null)
				leftTopWidthHeight = new int[] {0, 0, 350, 200};
			leftTopWidthHeight[2] = (int) (leftTopWidthHeight[2] * 0.8);
			leftTopWidthHeight[3] = 350;
			window.place(leftTopWidthHeight);
			window.show();
		}		
		
		private void initialiseControls() {
			List columns = ArrayListViewDataSource.prepareSingletonColumns("Name");
			List shareImgs = ArrayListViewDataSource.prepareApplImageSet("sharing-security-x64.png", "sharing-security-x16.png"); 
			List groupImgs = ArrayListViewDataSource.prepareApplImageSet("group-management-x64.png", "group-management-x16.png"); 
			List images = new ArrayList();
			data.add("Sharing and security");
			images.add(shareImgs);
			data.add("Group management");
			images.add(groupImgs);
			List dataSet = ArrayListViewDataSource.prepareArrayListDataset(data);
			lvDataSet = new ArrayListViewDataSource(columns, dataSet, images);
		}
		
		public Object getModel() {
			return lvDataSet;
		}
		
		public void onFormResized() {
			super.onFormResized();
			window.resizeListView();
		}

		public void onClick(ListView vw, int dataRow, int col) {
			// TODO Auto-generated method stub
			
		}

		public void onDblClick(ListView vw, int dataRow, int col) {
			String type = (String) data.get(dataRow);
			System.out.println("dblclicked: " + type);
		}
	}
}
